package com.assets.Check.newTanWanCheck;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.assets.Check.Check_choose;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.bean.Check_choose_bean;
import com.assets.databinding.NewCheckChooseBinding;
import com.assets.newNHF.util.LogUtils;
import com.assets.util.MyOkHttp;
import com.assets.util.Since_case;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

/**
 * Created by Administrator on 2019/5/14.
 */

public class New_Check_choose extends BaseActivity1 {
    private NewCheckChooseBinding binding;
    private String url = "http://" + Url.IP + "/api/checkdetailQueryById";
    private CheckAdapter adapter;
    private ArrayList<ItemCheckData> dataList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.new_check_choose);
        initView();
//        getCheckDetail();
    }

    private void initView() {
        binding.includeTitle.fh.setOnClickListener(v -> finish());
        binding.includeTitle.titleText.setText("选择事业部");
        binding.rcView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CheckAdapter(this, dataList);
        adapter.setRequest(position -> {
            String checkStatus = String.valueOf(Integer.parseInt(getIntent().getStringExtra("checkStatus")) + 1);
            LogUtils.d(checkStatus);
            if (!TextUtils.isEmpty(checkStatus)) {
                if (checkStatus.equals("2")) {
                    Intent intent = new Intent(this, Check_choose.class);
                    intent.putExtra("checkId", getIntent().getStringExtra("checkId"));
                    intent.putExtra("deptId", dataList.get(position).getDeptId());
                    intent.putExtra("checkStatus", checkStatus);
                    startActivity(intent);
                    finish();
                    return;
                }
            }
            Intent intent = new Intent(this, New_Check_choose.class);
            intent.putExtra("checkId", getIntent().getStringExtra("checkId"));
            intent.putExtra("checkStatus", checkStatus);
            startActivity(intent);
        });
        binding.rcView.setAdapter(adapter);
    }

    private void getCheckDetail() {
        showDialog("数据加载中");
        Map<String, String> map = new HashMap<>();
        map.put("checkStatus", getIntent().getStringExtra("checkStatus"));
        map.put("userId", Since_case.getInstance().getUserId());
        map.put("companyId", Since_case.getInstance().getCompany());
        map.put("checkId", getIntent().getStringExtra("checkId"));
        MyOkHttp.getInstance().post(url, map, new MyOkHttp.requestMessage() {
            @Override
            public void requestFail() {
                LogUtils.d("error");
                showToast("连接服务器失败");
            }

            @Override
            public void requestComplete() {
                canDialog();
            }

            @Override
            public void requestSuccess(Response response) throws IOException {
                String responseData = response.body().string();
                LogUtils.d(responseData + getIntent().getStringExtra("checkStatus"));
                List<Check_choose_bean> beanList = MyOkHttp.getInstance().getGson().fromJson(responseData, new TypeToken<List<Check_choose_bean>>() {
                }.getType());
                dataList.clear();
                for (Check_choose_bean bean : beanList) {
                    dataList.add(new ItemCheckData("事业部", bean.getDept(), bean.getResult(), bean.getType(), bean.getPlannum(), bean.getDeptId()));
                }
                runOnUiThread(() -> {
                    adapter.notifyDataSetChanged();
                });
            }
        });
    }
}
